<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

/**
 * 供应商页面布局 edit by kim
 * @auther 
 * @version 0.1
 * @package b2c_lib_finder
 */
class b2c_finder_business_basicmsg{
    var $detail_basic;
    var $detail_main;
//	var $detail_members;
    var $column_edit;
//    var $edit_msg;

    public function __construct($app)
    {
        $this->app = $app;

		$this->detail_basic = app::get('b2c')->_('供应商基础信息');
        $this->detail_main = app::get('b2c')->_('主账号信息');
//		$this->detail_members = app::get('b2c')->_('子账号列表');
		$this->detail_subaccount = app::get('b2c')->_('子账号列表');
        $this->detail_brand = app::get('b2c')->_('选择品牌');
		$this->detail_operator = app::get('b2c')->_('选择集货员');
        $this->column_edit = app::get('b2c')->_('操作');
//        $this->edit_msg = app::get('b2c')->_('修改基础信息');
    }
    function detail_basic($id){
        $render = app::get('b2c')->render();
        $oitem = kernel::single("b2c_mdl_business_basicmsg");
        $items = $oitem->getList('*',array('business_id' => $id), 0, 1);
        $render->pagedata['businessData'] = $items[0];
        return $render->fetch('admin/business/basicmsg.html');
    }

    function  detail_main($business_id = null){
        $render = app::get('b2c')->render();
        $oitem = kernel::single("b2c_mdl_business_basicmsg");
        $items = $oitem->getList('member_id',array('business_id' => $business_id), 0, 1);
        $member_id = $items[0];
        
        //pam_members
        $pmembers = app::get('pam')->model('members');
        $items = $pmembers->getList('*',array('member_id'=>$member_id),0,1);
        
        //b2c_members
        $bmembers = app::get('b2c')->model('members');
        $info = $bmembers->getList('*',array('member_id'=>$member_id),0,1);
        
        $newarr['pmembers'] = $items[0];
        $newarr['bmembers'] = $info[0];
        $render->pagedata['businessData'] = $newarr;
        return $render->fetch('admin/business/main_members.html');
    }

//    function detail_members($business_id = null){
//        $render = app::get('b2c')->render();
//        $pmembers = app::get('pam')->model('members');
//        $bmembers = app::get('b2c')->model('members');
//        $items = $bmembers -> getList('*',array('business_id'=>$business_id,'is_main'=>'2'));
//        
//        //根据符合条件的member_id去pam里面查名字，组成插进数组
//        foreach($items as $k=>$v){
//            $ids[] = $items[$k]['member_id'];
//            $paccounts = $pmembers -> getRow('login_account,member_id',array('member_id'=>$ids));
//            $items[$k]['account']= $paccounts['login_account'];
//        }
//        $render->pagedata['businessData'] = $items;
//        $render->pagedata['bid'] = $business_id;
//        return $render->fetch('admin/business/son_members.html');
//    }

	//子账号列表bykim 20150929
	function detail_subaccount($business_id = null){
		$render = app::get('b2c')->render();
		$member_lv=app::get('b2c')->model("member_lv");
        foreach($member_lv->getMLevel() as $row){
            $options[$row['member_lv_id']] = $row['name'];
        }
        $membersData['lv']['options'] = is_array($options) ? $options : array(app::get('b2c')->_('请添加会员等级')) ;

		$pmembers = app::get('pam')->model('members');
        $bmembers = app::get('b2c')->model('members');
        $items = $bmembers -> getList('*',array('business_id'=>$business_id,'is_main'=>'2'));
        
        //根据符合条件的member_id去pam里面查名字，组成插进数组
        foreach($items as $k=>$v){
            $ids[] = $items[$k]['member_id'];
            $paccounts = $pmembers -> getRow('login_account,member_id',array('member_id'=>$ids));
            $items[$k]['account']= $paccounts['login_account'];
        }
		$render->pagedata['mem'] = $membersData;
		$render->pagedata['bid'] = $business_id;
        $render->pagedata['businessData'] = $items;
		return $render->fetch('admin/business/sub_account.html');
	}

    function detail_brand($business_id=null){
        $render = app::get('b2c')->render();
        $brand_model = app::get('b2c')->model('brand');
        $bb_model = app::get('b2c')->model('business_brand');

        //将已绑定该供应商的品牌显示出来
        $brand_ids = $bb_model->getList('brand_id',array('business_id'=>$business_id));
        if($brand_ids){
            foreach($brand_ids as $k=>$v){
                $newarr[] = $brand_ids[$k]['brand_id']; 
            }
            $lists = $brand_model->getList('brand_id',array('has_bind'=>'Y','brand_id'=>$newarr));
            foreach($lists as $k=>$v){
                $lists1[] = $lists[$k]['brand_id'];
            }
            $render->pagedata['lists']=$lists1;

            //过滤品牌--显示未绑定的以及该品牌下已绑定的
            $arr = $brand_model->getList('brand_id',array('has_bind'=>'N'));
            if($arr){
                foreach($arr as $k=>$v){
                    $arr1[] = $arr[$k]['brand_id'];
                }
                $idss = array_merge($arr1,$lists1);
            }else{
                $idss = $lists1;
            }
            $render->pagedata['brand_filter'] = array('brand_id'=>$idss);
        }else{
            $arr = $brand_model->getList('brand_id',array('has_bind'=>'N'));
            foreach($arr as $k=>$v){
                $idss[] = $arr[$k]['brand_id'];
            }
            //当此处无品牌可选时
            if(empty($idss)){
                $render->pagedata['brand_filter'] = array('goods_id|noequal'=>'Y');
            }else{
                $render->pagedata['brand_filter'] = array('brand_id'=>$idss);
            }
        }
        $render->pagedata['business_id'] = $business_id;
        return $render->fetch('admin/business/choice_brand.html');
    }

	public function detail_operator($business_id=null){
		$render = app::get('b2c')->render();
		$render->pagedata['filter'] = array('user_type'=>2);
		$render->pagedata['business_id'] = $business_id;
		$lists = $this->app->model('business_operator')->getList('operator_id',array('business_id'=>$business_id));
		foreach($lists as $k=>$v){
			$lists1[] = $lists[$k]['operator_id'];
        }
		$render->pagedata['lists']=$lists1;
		return $render->fetch('admin/business/operator.html');
	}

    public function column_edit($row){
        $render = $this->app->render();
        $arr = array(
            'app'=>$_GET['app'],
            'ctl'=>$_GET['ctl'],
            'act'=>$_GET['act'],
            'finder_id'=>$_GET['_finder']['finder_id'],
            'action'=>'detail',
            'finder_name'=>$_GET['_finder']['finder_id'],
        );
        $arr_link = array(
            'info'=>array(
                'detail_basic'=>array(
                    'href'=>'javascript:void(0);',
                    'submit'=>'index.php?'.utils::http_build_query($arr).'&finderview=detail_basic&id='.$row['business_id'].'&_finder[finder_id]='.$_GET['_finder']['finder_id'],'label'=>app::get('b2c')->_('供应商基础信息'),
                    'target'=>'tab',
                ),
            ),
            'finder'=>array(
                'detail_main'=>array(
					'href'=>'javascript:void(0);',
                    'submit'=>'index.php?'.utils::http_build_query($arr).'&finderview=detail_main&id='.$row['business_id'].'&_finder[finder_id]='.$_GET['_finder']['finder_id'],'label'=>app::get('b2c')->_('主账号信息'),
                    'target'=>'tab',
                ),
                'detail_members'=>array(
					'href'=>'javascript:void(0);',
                    'submit'=>'index.php?'.utils::http_build_query($arr).'&finderview=detail_members&id='.$row['business_id'].'&_finder[finder_id]='.$_GET['_finder']['finder_id'],'label'=>app::get('b2c')->_('子账号列表'),
                    'target'=>'tab',
                ),
                'detail_brand'=>array(
					'href'=>'javascript:void(0);',
                    'submit'=>'index.php?'.utils::http_build_query($arr).'&finderview=detail_brand&id='.$row['business_id'].'&_finder[finder_id]='.$_GET['_finder']['finder_id'],'label'=>app::get('b2c')->_('选择品牌'),
                    'target'=>'tab',
                ),
				'detail_operator'=>array(
					'href'=>'javascript:void(0);',
                    'submit'=>'index.php?'.utils::http_build_query($arr).'&finderview=detail_operator&id='.$row['business_id'].'&_finder[finder_id]='.$_GET['_finder']['finder_id'],'label'=>app::get('b2c')->_('选择集货员'),
                    'target'=>'tab',
				),
            ),
            'mine'=>array(
                'edit_msg'=>array(
                    'href'=>'index.php?app=b2c&ctl=admin_business_basicmsg&act=edit&bid='.$row['business_id'].'&finder_id='.$_GET['_finder']['finder_id'],
                    'label'=>app::get('b2c')->_('修改基础信息'),
                    'target'=>'blank',
                ),
            ),
        );
        $render->pagedata['arr_link'] = $arr_link;
        $render->pagedata['handle_title'] = app::get('b2c')->_('编辑');
        $render->pagedata['is_active'] = 'true';
        return $render->fetch('admin/actions.html');
    }
}
